//
//  SEPatientAddViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/15.
//

import HandyJSON

enum SEPatientSex: Int, HandyJSONEnum {
    case male = 1
    case female = 2
    case unknown = 0

    var title: String {
        switch self {
        case .male:
            return "男"
        case .female:
            return "女"
        case .unknown:
            return "未知"
        }
    }
    
    var backgroundImage: UIImage? {
        switch self {
        case .male:    return Asset.iconFileBgMale.image
        case .female:  return Asset.iconFileBgFemale.image
        case .unknown: return nil
        }
    }
    
    var backgroundColor: UIColor? {
        switch self {
        case .male:     return UIColor(hex: "#C2E6FE")
        case .female:   return UIColor(hex: "#FDDAB6")
        case .unknown:  return nil
        }
    }
    
    var borderColor: CGColor {
        switch self {
        case .male:     return UIColor(hex: "#CFE7F7").cgColor
        case .female:   return UIColor(hex: "#F6E0C9").cgColor
        case .unknown:  return Asset.background.color.cgColor
        }
    }
}

enum SEPatientSmokingType: Int {
    case often = 0
    case once
    case never

    var selectedIndex: Int {
        switch self {
        case .often: return 0
        case .once: return 1
        case .never: return 2
        }
    }
}

enum SEPatientDrinkingType: Int {
    case often = 0
    case once
    case never

    var selectedIndex: Int {
        switch self {
        case .often: return 0
        case .once: return 1
        case .never: return 2
        }
    }
}

enum SEPatientAllergyType: Int {
    case no = 0
    case yes

    var selectedIndex: Int {
        switch self {
        case .no: return 1
        case .yes: return 0
        }
    }
}

enum SEPatientRelationship: Int {
    case myself = 0
    case parents
    case children
    case lover
    case other
    case unknowm

    var text: String {
        switch self {
        case .myself: return "自己"
        case .parents: return "父母"
        case .children: return "子女"
        case .lover: return "爱人"
        case .other: return "其他"
        case .unknowm: return ""
        }
    }
}

class SEPatientDetailViewModel: SEBaseViewModel {
    var model = SEPatientDetailModel()

    var authViewModel = SEAuthViewModel()

    convenience init(model: SEPatientDetailModel) {
        self.init()
        self.model = model
        originalPhone = model.phone
    }

    var editType: SEPatientDetailViewControllerEditType = .add

    var submitHandler: ((SEHttpStatus) -> Void)?

    var msgCodeHandler: ((SEHttpStatus) -> Void)?

    var originalPhone: String = ""
    var smokingType: SEPatientSmokingType {
        return SEPatientSmokingType(rawValue: model.isSmoking) ?? .never
    }
    var dringkingType: SEPatientDrinkingType{
        return SEPatientDrinkingType(rawValue: model.isDrinking) ?? .never
    }
    var allergyType: SEPatientAllergyType {
        return SEPatientAllergyType(rawValue: model.isAllergy) ?? .no
    }

    var relationship: SEPatientRelationship {
        SEPatientRelationship(rawValue: model.relation) ?? .unknowm
    }
    var sexStr: String { model.sex.title }
    var idNumberStr: String {
        guard model.idNumber.count > 14 else { return "" }
        let subStr = model.idNumber.subString(index: 6, length: 8)
        return model.idNumber.replacingOccurrences(of: subStr, with: "********")
    }
    var backgroundImage: UIImage? { model.sex.backgroundImage }
    var onselfBackgroundColor: UIColor? { model.sex.backgroundColor }
    var borderColor: CGColor { model.sex.borderColor }

    var administrative: String {
        if model.provinceName.isEmpty || model.cityName.isEmpty || model.regionName.isEmpty {
            return ""
        } else {
            return "\(model.provinceName),\(model.cityName),\(model.regionName)"
        }
    }

    var dataSource: [[SEPatientTitleTextFieldCellModel]] {
        return [
            [
                SEPatientTitleTextFieldCellModel(title: "姓名",
                                                 placeHolder: "请输入姓名",
                                                 cellType: .normal,
                                                 text: model.name),
                SEPatientTitleTextFieldCellModel(title: "身份证号",
                                                 placeHolder: "请输入身份证号",
                                                 cellType: .normal,
                                                 text: model.idNumber,
                                                 editEnable: editType == .add),
                SEPatientTitleTextFieldCellModel(title: "行政区域",
                                                 placeHolder: "请选择您所在区域",
                                                 cellType: .tap,
                                                 text: administrative),
                SEPatientTitleTextFieldCellModel(title: "详细地址",
                                                 placeHolder: "请输入详细地址",
                                                 cellType: .normal,
                                                 text: model.addr),
                SEPatientTitleTextFieldCellModel(title: "联系电话",
                                                 placeHolder: "请输入联系电话",
                                                 cellType: .normal,
                                                 text: model.phone),
                SEPatientTitleTextFieldCellModel(title: "验证码",
                                                 placeHolder: "请输入验证码",
                                                 cellType: .code,
                                                 text: model.msgCode),
                SEPatientTitleTextFieldCellModel(title: "与我关系",
                                                 placeHolder: "请选择",
                                                 cellType: .tap,
                                                 text: relationship.text),
            ],
            [
                SEPatientTitleTextFieldCellModel(title: "身高",
                                                 placeHolder: "请输入",
                                                 cellType: .unit,
                                                 unit: "cm",
                                                 text: model.height),
                SEPatientTitleTextFieldCellModel(title: "体重",
                                                 placeHolder: "请输入",
                                                 cellType: .unit,
                                                 unit: "kg",
                                                 text: model.weight),
                SEPatientTitleTextFieldCellModel(title: "是否吸烟",
                                                 cellType: .segment,
                                                 segmentArray: ["经常", "偶尔", "否"],
                                                 segmentSelectedIndex: smokingType.selectedIndex),
                SEPatientTitleTextFieldCellModel(title: "是否饮酒",
                                                 cellType: .segment,
                                                 segmentArray: ["大量", "少量", "否"],
                                                 segmentSelectedIndex: dringkingType.selectedIndex),
                SEPatientTitleTextFieldCellModel(title: "是否有过敏史",
                                                 placeHolder: "",
                                                 cellType: .segment,
                                                 segmentArray: ["是", "否"],
                                                 segmentSelectedIndex: allergyType.selectedIndex),
            ],
            [
                SEPatientTitleTextFieldCellModel(title: "既往史",
                                                 cellType: .title),
            ]
        ]
    }

    var checkSubmitPatientDetail: String? {
        if model.name.isEmpty {
            return "请输入姓名"
        }
        if model.idNumber.isEmpty {
            return "请输入身份证号"
        }
        if model.province.isEmpty || model.city.isEmpty || model.region.isEmpty {
            return "请选择您所在区域"
        }
        if model.addr.isEmpty {
            return "请输入详细地址"
        }
        if model.phone.isEmpty {
            return "请输入联系电话"
        }
        if model.msgCode.isEmpty && originalPhone != model.phone {
            return "请输入验证码"
        }
        if relationship == .unknowm {
            return "请选择与我关系"
        }
        return nil
    }

    /// 患者详情
    func requestPatientDetail() {
        SEHttpManager.shared.request(SEProfileAPI.patientDetail(id: model.id), model: SEPatientDetailModel.self, withLoading: true) { (model, status) in
            switch status {
            case .success:
                self.model = model ?? SEPatientDetailModel()
            default: break
            }
            self.updateData?(status)
        }
    }

    /// 新增患者
    func submitPatientDetail() {
        guard checkSubmitPatientDetail == nil else {
            SEHUD.showError(message: checkSubmitPatientDetail!)
            return
        }
        SEHttpManager.shared.requestForAny(SEProfileAPI.submitPatient(viewModel: self, isEdit: editType == .edit), withLoading: true) { (dataStr, status) in
            self.submitHandler?(status)
        }
    }

    /// 获取验证码
    func requestMsgCode() {
        authViewModel.account = model.phone
        authViewModel.requestMsgCode()
        authViewModel.msgCodeHandler = msgCodeHandler
    }

}

